package com.wind.homework.controller;


import com.wind.homework.mapper.InfoMapper;

import com.wind.homework.tools.PaHtml;
import com.wind.homework.tools.ZipUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@RestController
@RequestMapping("/aaa")
public class UrlController {

    @Autowired
    InfoMapper infoMapper;

    @GetMapping("/bbb")
    public void abc(){
        URL url = null;
        URLConnection urlconn = null;
        BufferedReader br = null;
        String regex = "/sthjt/c100456/2.*shtml";//url匹配规则
        Pattern p = Pattern.compile(regex);
        List<String> list = new ArrayList();
        try {
            url = new URL("http://sthjt.sc.gov.cn/sthjt/c100456/zclistf_2.shtml");//爬取的网址
            urlconn = url.openConnection();
//            pw = new PrintWriter(new FileWriter("/Users/gaox/Desktop/url"), true);//将爬取到的链接存到本地文件
            br = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
            String buf = null;

            while ((buf = br.readLine()) != null) {
                Matcher buf_m = p.matcher(buf);

                while (buf_m.find()) {
                    String abc = "http://sthjt.sc.gov.cn"+buf_m.group();
                    list.add(abc);
//                    System.out.println(abc);
//                    PaHtml paHtml = new PaHtml();
//                    paHtml.method(abc);
//                    String sourceFileName = "/Users/gaox/Desktop/url/"+abc.substring(abc.length()-34,abc.length()-5)+"html";
//                    String zipFileName = "/Users/gaox/Desktop/url/"+abc.substring(abc.length()-34,abc.length()-5)+"zip";
//
//                    ZipUtil zipUtil = new ZipUtil();
//                    zipUtil.zip(zipFileName, sourceFileName);


                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
//            pw.close();
        }


//        list.add("http://sthjt.sc.gov.cn/sthjt/c100456/2020/4/29/9b462a62c78e4b8387155b4676536c8a.shtml");
//        list.add("http://sthjt.sc.gov.cn/sthjt/c100456/2020/4/3/e157b30964474ec583dbdecaa97bd69e.shtml");
//        list.add("http://sthjt.sc.gov.cn/sthjt/c100456/2020/3/31/ca45f27a5d1f4f1bb6ab6089c65b22d2.shtml");
//        list.add("http://sthjt.sc.gov.cn/sthjt/c100456/2020/3/30/f740957319c94d08b96536b4c3e235ec.shtml");
//        list.add("http://sthjt.sc.gov.cn/sthjt/c100456/2020/3/9/cbbb40313b064ee8b3e4f59f4c600587.shtml");
//        list.add("http://sthjt.sc.gov.cn/sthjt/c100456/2020/3/9/5526b32cbc8641f7bdfa782f4e95da62.shtml");
//        list.add("http://sthjt.sc.gov.cn/sthjt/c100456/2020/3/9/73fca47471554340b254600a28c4a0fb.shtml");
//        list.add("http://sthjt.sc.gov.cn/sthjt/c100456/2020/3/6/698870ca88ee4a00bcd0ae5382f2e683.shtml");
//        list.add("http://sthjt.sc.gov.cn/sthjt/c100456/2020/1/19/6b3cf13701d74b098ddda3fc35bf99c2.shtml");
//        list.add("http://sthjt.sc.gov.cn/sthjt/c100456/2020/1/17/9228c351ae954e9c8a221cb418ee486b.shtml");
//        list.add("http://sthjt.sc.gov.cn/sthjt/c100456/2019/11/4/297427a0bbff4ddeadbdd4aa723454af.shtml");
//        list.add("http://sthjt.sc.gov.cn/sthjt/c100456/2019/11/4/81ae947734cf41f185edfb133da0fb4f.shtml");


        for(int i=0;i<list.size();i++){
            method(list.get(i));
        }
    }

    public String method(String souceUrl) {


        URL url = null;
        URLConnection urlconn = null;
        BufferedReader br = null;
        PrintWriter pw = null;

        String regex = ">.*?</td>";//url匹配规则

        Pattern p = Pattern.compile(regex);
        List<String> list = new ArrayList<String>();
        try {
            url = new URL(souceUrl);//爬取的网址
            urlconn = url.openConnection();
            // pw = new PrintWriter(new FileWriter("/Users/gaox/Desktop/url/url.txt"), true);//将爬取到的链接存到本地文件
            br = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
            String buf = null;

            while ((buf = br.readLine()) != null) {
                Matcher buf_m = p.matcher(buf);
                while (buf_m.find()) {
                    //pw.println("http://sthjt.sc.gov.cn"+buf_m.group());
                    int len = buf_m.group().length();
                    String res = buf_m.group().substring(1, len-5);
                    list.add(res);                   //爬取结果放入list
                    //System.out.println(res);
                }
            }
            System.out.println("爬取成功^_^");
            String illegalCase = list.get(0);
            String company=null,nature=null,personCode=null,personName=null,caseCode=null,illegalType=null,illegalFacts=null,
                    cfyj=null,cflb=null,cfnr=null,cfrq=null,cfyxq=null,cfjzq=null,cfjg=null,cfjgCode=null,dataFrom=null,
                    dataFromCode=null,jycf=null,beizhu=null;

//          把font过滤掉
            for(int i=1;i<list.size()-4;i++) {
                if(list.get(i).contains("font")) {
                    Pattern pa = Pattern.compile(">.*?<");
                    Matcher ma = pa.matcher(list.get(i));
                    String str =null;
                    while(ma.find()) {
                        str = str+ma.group();
                    }
                    Collections.replaceAll(list, list.get(i), str);
                }else {
                    continue;
                }
            }

            System.out.println(list);

//          把结果对应起来
            for(int i=1;i<list.size()-4;i++) {
                if(list.get(i).equals("行政相对人名称")) {
                    company = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("行政相对人类别")) {
                    nature = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("统一社会信用代码")) {
                    personCode = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("法定代表人")) {
                    personName = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("行政处罚决定书文号")) {
                    caseCode = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("违法行为类型")) {
                    illegalType = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("违法事实")) {
                    illegalFacts = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("处罚依据")) {
                    cfyj = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("处罚类别")) {
                    cflb = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("处罚内容")) {
                    cfnr = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("处罚决定日期")) {
                    cfrq = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("处罚有效期")) {
                    cfyxq = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("公示截止期")) {
                    cfjzq = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("处罚机关")) {
                    cfjg = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("处罚机关统一社会信用代码")) {
                    cfjgCode = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("数据来源单位")) {
                    dataFrom = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("数据来源单位统一社会信用代码")) {
                    dataFromCode = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("是否为简易程序处罚")) {
                    jycf = list.get(i+1);
                    continue;
                }else if (list.get(i).equals("备注")) {
                    beizhu = list.get(i+1);
                    continue;
                }
                //System.out.println(a);
            }
            //System.out.println(jycf);

            System.out.println(illegalCase);
//
            infoMapper.insertInfo(illegalCase,company,nature,personCode,personName,caseCode,illegalType,illegalFacts,cfyj,cflb,cfnr,cfrq,cfyxq,cfjzq,cfjg,cfjgCode,dataFrom,dataFromCode,jycf,beizhu);
            System.out.println("存储成功^_^");
            return "存储成功";

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "运行结束";
            //pw.close();
        }


    }

}
